package com.cameron.serviceedu.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cameron.serviceedu.entity.EduCourse;
import com.cameron.serviceedu.entity.EduSubject;
import com.cameron.serviceedu.entity.dto.ChapterDto;
import com.cameron.serviceedu.entity.dto.front.*;
import com.cameron.serviceedu.service.*;
import com.cameron.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@CrossOrigin
@RequestMapping("/service/course-front")
@RestController
@Slf4j
@SuppressWarnings("unchecked")
public class CourseController {

    @Resource
    private RedisTemplate<String, Map<String, Object>> redisTemplate;
    @Resource
    private EduCourseService courseService;

    @Resource
    private EduChapterService chapterService;

    @Resource
    private EduSubjectService subjectService;


    //条件分页查询
    @PostMapping("getCourseList/{currentPage}/{pageSize}")
    public Result pageByRules(@PathVariable int currentPage, @PathVariable int pageSize,
                              @RequestBody(required = false) CourseDto courseDto) {
        log.info("get->>{}", courseDto);
        log.info("get->>{}", new CourseDto().equals(courseDto));
        if (courseDto == null || new CourseDto().equals(courseDto)) {
            String key = "online:edu:courseList:" + currentPage + ":" + pageSize;
            Map<String, Object> resMap = redisTemplate.opsForValue().get(key);
            if (CollectionUtils.isEmpty(resMap)) {
                Map<String, Object> courseList = courseService.getCourseList(currentPage, pageSize, null);
                redisTemplate.opsForValue().set(key, courseList, 30, TimeUnit.MINUTES);
                resMap = courseList;
            }
            log.info("request goes to cache!");
            return Result.ok().data(resMap);
        }
        Map<String, Object> map = courseService.getCourseList(currentPage, pageSize, courseDto);
        return Result.ok().data(map);
    }

    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId) {
        log.info("courseID::::{}", courseId);
        CourseInfoVo courseInfo = courseService.getCourseInfo(courseId);
        List<ChapterDto> chapterList = chapterService.getAll(courseId);
        return Result.ok().data("courseWebVo", courseInfo).data("chapterVideoList", chapterList);
    }

    @GetMapping("getAllSubject")
    public Result getAllSubject() {
        LambdaQueryWrapper<EduSubject> lqw = new LambdaQueryWrapper<>();
        lqw.eq(EduSubject::getParentId, '0');
        List<EduSubject> list = subjectService.list(lqw);

        List<SubjectDto> subjectDtoList = list.stream().map((item) -> {
            //将一级分类拷贝到返回结果类中
            SubjectDto subjectDto = new SubjectDto();
            BeanUtils.copyProperties(item, subjectDto);

            //通过一级分类id查询其子分类
            LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
            String parentId = item.getId();
            wrapper.eq(EduSubject::getParentId, parentId);
            List<EduSubject> childrenList = subjectService.list(wrapper);

            //将子分类添加到返回结果中
            subjectDto.setChildren(childrenList);
            return subjectDto;
        }).collect(Collectors.toList());
        return Result.ok().data("list", subjectDtoList);
    }
/*
    @GetMapping("getOrderInfo")
    public OrderCourseDto getOrderInfo(String courseId, HttpServletRequest request) {
        return courseService.getOrderCourseInfo(courseId, request);
    }*/

    @GetMapping("getCourseList/like/{courseName}")
    public Result getCourseList(@PathVariable String courseName) {
        List<EduCourse> courseList = courseService.getCourseLikeName(courseName);
        return Result.ok().data("courseList", courseList);
    }
}
